#include "mobilefilesecondwidget.h"

#include <QVBoxLayout>
#include <QScrollArea>
#include <QDebug>
#include <kbreadcrumb.h>
#include "generatetools.h"

using namespace kdk;

const QString ITEM_ICON_PATH = ":/icon/";
const QStringList ITEM_ICON_LIST = {"pictureIcon.png", "videoIcon.png", "musicIcon.png", "documentIcon.png"};
const QStringList ITEM_NAME_LIST = {"Picture", "Video", "Music", "Document"};
const int ICON_MODE_LIST_WIDGET_WIDTH = 240;
const int LIST_WIDGET_HEIGHT = 420;
const int LIST_WIDGET_ITEM_WIDTH = 205;
const int LIST_WIDGET_ITEM_HEIGHT = 88;
const int LIST_MODE_LIST_WIDGET_WIDTH = 750;

MobileFileSecondWidget::MobileFileSecondWidget(int text, QWidget *parent) : QWidget(parent)
{
    m_text = text;
    initUI();
    setItem();
}

MobileFileSecondWidget::~MobileFileSecondWidget()
{
    if (m_fileSecondListWidget != nullptr) {
        delete m_fileSecondListWidget;
        m_fileSecondListWidget = nullptr;
    }
}

void MobileFileSecondWidget::setItemCount(const QMap<QString, int> &map)
{
    if (map.isEmpty()) {
        return;
    }
    QMap<QString, int>::const_iterator it;
    QMap<QString, MobileFileListItem *>::iterator itemIt = m_itemMap.begin();
    MobileFileListItem *item = nullptr;
    while (m_itemMap.end() != itemIt) {
        item = itemIt.value();
        it = map.find(itemIt.key());
        item->setItemCount(it.value());
        itemIt++;
    }
}

void MobileFileSecondWidget::initUI()
{
    QVBoxLayout *mainVLayout = new QVBoxLayout();



    QHBoxLayout *fileListHLayout = new QHBoxLayout();

    QPushButton *retreatButton = new QPushButton(this);
    retreatButton->setIcon(QIcon::fromTheme("go-next-rtl-symbolic"));
    retreatButton->setFixedSize(16, 16);
    retreatButton->setFlat(true);
    retreatButton->setCursor(Qt::PointingHandCursor);

    QFont font;
    font.setBold(true);
    //面包屑
    KBreadCrumb *crumb = new KBreadCrumb(this);
    crumb->setFont(font);
    crumb->setFlat(false);
    crumb->addTab(tr("Mobile file list")); // Mobile file list手机文件列表
    if (m_text == SECOND_FILE_LIST_WIN_TYPE::WECHAT) {
        crumb->addTab(tr("WeChat")); // WeChat
    } else if (m_text == SECOND_FILE_LIST_WIN_TYPE::QQ) {
        crumb->addTab(tr("QQ")); // QQ
    }
    crumb->setCurrentIndex(1);
    crumb->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    //模式
    m_modeButton = new QPushButton(this);
    m_modeButton->setIcon(QIcon::fromTheme("view-grid-symbolic"));
    m_modeButton->setToolTip(tr("List Mode")); // List Mode列表模式
    m_modeButton->setCursor(Qt::PointingHandCursor);
    m_modeButton->setFixedSize(20, 20);
    m_modeButton->setFlat(true);

    //刷新
    QPushButton *refreshButton = new QPushButton(this);
    refreshButton->setIcon(QIcon::fromTheme("view-refresh-symbolic"));
    refreshButton->setToolTip(tr("Refresh")); // Refresh刷新
    refreshButton->setCursor(Qt::PointingHandCursor);
    refreshButton->setFixedSize(20, 20);
    refreshButton->setFlat(true);

    fileListHLayout->addWidget(retreatButton);
    fileListHLayout->addWidget(crumb);
    fileListHLayout->addStretch();
    fileListHLayout->addWidget(m_modeButton);
    fileListHLayout->addSpacing(20);
    fileListHLayout->addWidget(refreshButton);
    fileListHLayout->setContentsMargins(67, 0, 68, 0);

    m_fileSecondListWidget = new QListWidget();
    //设置显示图标模式
    m_fileSecondListWidget->setViewMode(QListView::IconMode);
    //自适应布局
    m_fileSecondListWidget->setResizeMode(QListView::Adjust);
    //列表中的图标不可拖动
    m_fileSecondListWidget->setMovement(QListView::Static);
    //去边框
    m_fileSecondListWidget->setFrameShape(QListWidget::NoFrame);

    QVBoxLayout *scrollVLayout = new QVBoxLayout();
    QVBoxLayout *layout = new QVBoxLayout();

    QWidget *listWidget = new QWidget();
    listWidget->setLayout(layout);

    scrollVLayout->addWidget(listWidget);
    scrollVLayout->setContentsMargins(67, 0, 0, 0);

    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(m_fileSecondListWidget);
    layout->addStretch();

    mainVLayout->addLayout(fileListHLayout);
    mainVLayout->addSpacing(24);
    mainVLayout->addLayout(scrollVLayout);
    mainVLayout->addStretch();

    mainVLayout->setContentsMargins(0, 0, 0, 0);
    this->setLayout(mainVLayout);

    connect(retreatButton, &QPushButton::clicked, this, [=]() {
        int currentIndex = crumb->currentIndex();
        if (currentIndex > 0) {
            crumb->setCurrentIndex(currentIndex - 1);
        }
    });

    connect(refreshButton, &QPushButton::clicked, this, [=]() {
        if (m_text == SECOND_FILE_LIST_TYPE::QQPICTURE) {
            Q_EMIT sigUpdateClicked(SECOND_FILE_LIST_TYPE::QQPICTURE);
        } else if (m_text == SECOND_FILE_LIST_TYPE::QQVIDEO) {
            Q_EMIT sigUpdateClicked(SECOND_FILE_LIST_TYPE::QQVIDEO);
        } else if (m_text == SECOND_FILE_LIST_TYPE::QQMUSIC) {
            Q_EMIT sigUpdateClicked(SECOND_FILE_LIST_TYPE::QQMUSIC);
        } else if (m_text == SECOND_FILE_LIST_TYPE::QQDOCUMENT) {
            Q_EMIT sigUpdateClicked(SECOND_FILE_LIST_TYPE::QQDOCUMENT);
        } else if (m_text == SECOND_FILE_LIST_TYPE::WECHATPICTURE) {
            Q_EMIT sigUpdateClicked(SECOND_FILE_LIST_TYPE::WECHATPICTURE);
        } else if (m_text == SECOND_FILE_LIST_TYPE::WECHATVIDEO) {
            Q_EMIT sigUpdateClicked(SECOND_FILE_LIST_TYPE::WECHATVIDEO);
        } else if (m_text == SECOND_FILE_LIST_TYPE::WECHATMUSIC) {
            Q_EMIT sigUpdateClicked(SECOND_FILE_LIST_TYPE::WECHATMUSIC);
        } else if (m_text == SECOND_FILE_LIST_TYPE::WECHATDOCUMENT) {
            Q_EMIT sigUpdateClicked(SECOND_FILE_LIST_TYPE::WECHATDOCUMENT);
        }
    });
    connect(m_modeButton, &QPushButton::clicked, this, &MobileFileSecondWidget::slotModeButtonClicked);
    connect(crumb, &KBreadCrumb::currentChanged, this, [=](int index) {
        QString text = crumb->tabText(index);
        if (index == 0 && text == tr("Mobile file list")) { // Mobile file list手机文件列表
            Q_EMIT sigHideCopyToComputerWidget();
            Q_EMIT sigShowMobileFileListWidget();
            Q_EMIT sigUpdateClicked(SECOND_FILE_LIST_WIN_TYPE::HOMEPAGE);
            return;
        }

        for (int i = crumb->count() - 1; i > index; i--) {
            crumb->removeTab(i);
        }

        Q_EMIT sigHideCopyToComputerWidget();
    });
}

void MobileFileSecondWidget::slotModeButtonClicked()
{
    // 切换视图埋点
    GenerateTools::buriedPoint(kabase::BuriedPoint::PT::KylinMobileAssistantSwitchView);
    switch (m_mode) {
    case 0:
        qInfo() << "列表模式";
        ++m_mode;
        m_modeButton->setIcon(QIcon::fromTheme("view-list-symbolic"));
        m_modeButton->setToolTip(tr("Icon Mode")); // Icon Mode图标模式
        m_fileSecondListWidget->setViewMode(QListView::ListMode);
        m_fileSecondListWidget->setFixedSize(ICON_MODE_LIST_WIDGET_WIDTH, LIST_WIDGET_HEIGHT);
        break;
    case 1:
        m_mode = 0;
        m_modeButton->setIcon(QIcon::fromTheme("view-grid-symbolic"));
        m_modeButton->setToolTip(tr("List Mode")); // List Mode列表模式
        m_fileSecondListWidget->setViewMode(QListView::IconMode);
        m_fileSecondListWidget->setFixedSize(LIST_MODE_LIST_WIDGET_WIDTH, LIST_WIDGET_HEIGHT);
        break;
    default:
        break;
    }
}

void MobileFileSecondWidget::setItem()
{
    for (int i = 0; i < 4; i++) {
        QListWidgetItem *item = new QListWidgetItem(m_fileSecondListWidget);
        MobileFileListItem *mobileFileListItem = new MobileFileListItem();
        m_fileSecondListWidget->setItemWidget(item, mobileFileListItem);
        item->setSizeHint(QSize(LIST_WIDGET_ITEM_WIDTH, LIST_WIDGET_ITEM_HEIGHT));
        mobileFileListItem->setItemCount(0);
        mobileFileListItem->setItemIcon(ITEM_ICON_PATH + ITEM_ICON_LIST.value(i));
        if (m_text == SECOND_FILE_LIST_WIN_TYPE::WECHAT) { // WeChat微信
            m_itemMap.insert("wechat" + ITEM_NAME_LIST.value(i), mobileFileListItem);
        } else {
            m_itemMap.insert("qq" + ITEM_NAME_LIST.value(i), mobileFileListItem);
        }
        switch (i) {
        case 0:
            mobileFileListItem->setItemName(tr("Picture")); // Picture图片
            connect(mobileFileListItem, &MobileFileListItem::clicked, this,
                    &MobileFileSecondWidget::slotPictureButtonClicked);
            break;
        case 1:
            mobileFileListItem->setItemName(tr("Video")); // Video视频
            connect(mobileFileListItem, &MobileFileListItem::clicked, this,
                    &MobileFileSecondWidget::slotVideoButtonClicked);
            break;
        case 2:
            mobileFileListItem->setItemName(tr("Music")); // Music音乐
            connect(mobileFileListItem, &MobileFileListItem::clicked, this,
                    &MobileFileSecondWidget::slotMusicButtonClicked);
            break;
        case 3:
            mobileFileListItem->setItemName(tr("Document")); // Document文档
            connect(mobileFileListItem, &MobileFileListItem::clicked, this,
                    &MobileFileSecondWidget::slotDocumentButtonClicked);
            break;
        default:
            break;
        }
    }
    m_fileSecondListWidget->setStyleSheet(
        "QListWidget::item{background-color:transparent;margin-right:35px;margin-bottom:20px;}");
}

void MobileFileSecondWidget::slotPictureButtonClicked()
{
    if (m_text == SECOND_FILE_LIST_WIN_TYPE::WECHAT) {
        Q_EMIT sigItemButtonClicked(SECOND_FILE_LIST_TYPE::WECHATPICTURE);
    } else {
        Q_EMIT sigItemButtonClicked(SECOND_FILE_LIST_TYPE::QQPICTURE);
    }
}

void MobileFileSecondWidget::slotVideoButtonClicked()
{
    if (m_text == SECOND_FILE_LIST_WIN_TYPE::WECHAT) {
        Q_EMIT sigItemButtonClicked(SECOND_FILE_LIST_TYPE::WECHATVIDEO);
    } else {
        Q_EMIT sigItemButtonClicked(SECOND_FILE_LIST_TYPE::QQVIDEO);
    }
}

void MobileFileSecondWidget::slotMusicButtonClicked()
{
    if (m_text == SECOND_FILE_LIST_WIN_TYPE::WECHAT) {
        Q_EMIT sigItemButtonClicked(SECOND_FILE_LIST_TYPE::WECHATMUSIC);
    } else {
        Q_EMIT sigItemButtonClicked(SECOND_FILE_LIST_TYPE::QQMUSIC);
    }
}

void MobileFileSecondWidget::slotDocumentButtonClicked()
{
    if (m_text == SECOND_FILE_LIST_WIN_TYPE::WECHAT) {
        Q_EMIT sigItemButtonClicked(SECOND_FILE_LIST_TYPE::WECHATDOCUMENT);
    } else {
        Q_EMIT sigItemButtonClicked(SECOND_FILE_LIST_TYPE::QQDOCUMENT);
    }
}
